import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class OrganizationAttendanceListPage extends StatefulWidget {
  const OrganizationAttendanceListPage({Key? key}) : super(key: key);

  @override
  State<OrganizationAttendanceListPage> createState() =>
      _OrganizationAttendanceListPageState();
}

class _OrganizationAttendanceListPageState
    extends State<OrganizationAttendanceListPage> {
  final controller = Get.find<OrganizationAttendanceListController>();
  final state = Get.find<OrganizationAttendanceListController>().state;

  var count = 0;

  @override
  Widget build(BuildContext context) {
    return MkPage(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: TDNavBar(
          backgroundColor: Colors.transparent,
          title: '我创建的考勤',
          rightBarItems: [
            TDNavBarItem(
                iconWidget: Icon(Icons.add),
                action: () {
                  Get.toNamed(RouterName.organizationAttendanceCreate,
                      arguments: Get.arguments);
                }),
          ],
        ),
        body: Obx(() => _buildBody()),
      ),
    );
  }

  _buildBody() {
    return Padding(
      padding: EdgeInsets.only(
          top: AppTheme.iDefaultPadding,
          left: AppTheme.iDefaultPadding,
          right: AppTheme.iDefaultPadding),
      child: EasyRefresh(
        // 下拉样式
        header: TDRefreshHeader(),
        footer: TaurusFooter(),
        child: ListView.builder(
          itemBuilder: (context, index) {
            return _buildItem(index);
          },
          itemCount: state.dataList.length,
        ),
        // 下拉刷新回调
        onRefresh: controller.onRefresh,
        onLoad: controller.onLoad,
      ),
    );
  }

  void doNothing(BuildContext context) {}

  Widget _buildItem(index) {
    return Slidable(
      // The end action pane is the one at the right or the bottom side.
      endActionPane: ActionPane(
        motion: ScrollMotion(),
        children: [
          SlidableAction(
            // An action can be bigger than the others.
            onPressed: (context) {
              controller.removeByIndex(index);
            },
            backgroundColor: Colors.redAccent,
            foregroundColor: Colors.white,
            icon: Icons.delete,
            label: '删除',
          ),
        ],
      ),
      child: GestureDetector(
        onTap: () {
          Get.toNamed(RouterName.organizationAttendanceDetail,arguments: state.dataList[index].id);
        },
        child: Container(
            width: double.infinity,
            decoration: BoxDecoration(
              color: Colors.white,
              border: new Border(
                bottom:
                    BorderSide(color: Colors.black.withOpacity(0.03), width: 1),
              ),
            ),
            padding: EdgeInsets.all(AppTheme.iDefaultPadding),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                MkLoadImage(
                  Assets.organizationApprove,
                  width: 40,
                ),
                Gaps.hGap8,
                Expanded(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Text(
                            '${state.dataList[index].title}',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 16),
                          ),
                          TDTag(
                            '2人',
                            theme: TDTagTheme.primary,
                            isOutline: true,
                            size: TDTagSize.small,
                          )
                        ],
                      ),
                      Gaps.vGap4,
                      Text(
                        '${state.dataList[index].taskStartTime} - ${state.dataList[index].taskEndTime}',
                        style: TextStyle(fontSize: 12, color: Color(0xFF606266)),
                      ),
                      Gaps.vGap4,
                      Text(
                        '${state.dataList[index].address}',
                        style: TextStyle(fontSize: 12, color: Color(0xFF606266)),
                      ),
                    ],
                  ),
                )
              ],
            )),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<OrganizationAttendanceListController>();
    super.dispose();
  }
}
